<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictTypeId">
        <el-select
          v-model="queryParams.dictTypeId"
          placeholder="字典类型"
          @change="handleDictTypeQuery"
        >
          <el-option
            v-for="item in dictTypeList"
            :key="item.dictId"
            :label="item.dictName"
            :value="item.dictId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字典数据" prop="dictDataId">
        <el-select v-model="queryParams.dictDataId" placeholder="字典类型">
          <el-option
            v-for="item in dictDataList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="标签" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入标签"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div class="all-button">
        <div class="left-button">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              :icon="Plus"
              @click="handleAdd"
              v-hasPerm="['bus:website:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              :icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPerm="['bus:website:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              :icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPerm="['bus:website:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              :icon="Download"
              @click="handleExport"
              v-hasPerm="['bus:website:export']"
              >导出</el-button
            >
          </el-col>
        </div>
        <div>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </div>
      </div>
    </el-row>

    <el-table
      v-loading="loading"
      :data="websiteList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column
        label="网站地址"
        align="center"
        prop="websiteUrl"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{ row }">
          <el-link type="danger" :href="row.websiteUrl" target="_blank">{{
            row.websiteUrl
          }}</el-link>
        </template>
      </el-table-column>
      <!-- <el-table-column label="阅读数" align="center" prop="readingCount" /> -->
      <el-table-column label="标签" align="center" prop="tags" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              :icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPerm="['bus:website:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              :icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPerm="['bus:website:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网址收录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="websiteRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="图标" prop="iconUrl">
              <el-input v-model="form.iconUrl" placeholder="请输入图标" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="websiteUrl">
              <el-input
                v-model="form.websiteUrl"
                placeholder="请输入地址"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="标签" prop="tags">
              <el-input
                v-model="form.tags"
                placeholder="请输入标签"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="字典类型" prop="dictTypeId">
              <el-select
                v-model="form.dictTypeId"
                placeholder="字典类型"
                @change="handleDictTypeQuery"
              >
                <el-option
                  v-for="item in dictTypeList"
                  :key="item.dictId"
                  :label="item.dictName"
                  :value="item.dictId"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="字典数据" prop="dictDataId">
              <el-select v-model="form.dictDataId" placeholder="字典类型">
                <el-option
                  v-for="item in dictDataList"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="文摘">
            <editor v-model="form.abstractContent" :min-height="192" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              /> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Website",
};
</script>
<script setup name="Website">
import {
  listWebsite,
  getWebsite,
  delWebsite,
  addWebsite,
  updateWebsite,
} from "@/api/bus/website";
import { listDictData } from "@/api/sys/dictData";
import { listDictType } from "@/api/sys/dictType";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import {
  Search,
  Plus,
  Refresh,
  Edit,
  Download,
  Delete,
} from "@element-plus/icons-vue";
const { proxy } = getCurrentInstance();
const dictTypeList = ref([]);
const dictDataList = ref([]);
const websiteList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    dictDataId: null,
    dictTypeId: null,
    tags: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询网址收录列表 */
function getList() {
  loading.value = true;
  listWebsite(queryParams.value).then((response) => {
    websiteList.value = response.data.rows;
    total.value = response.data.total;
    loading.value = false;
  });
  listDictType({ pageNum: 1, pageSize: 1000 }).then((response) => {
    dictTypeList.value = response.data.rows;
  });
}
function handleDictTypeQuery(e) {
  listDictData({
    pageNum: 1,
    pageSize: 1000,
    dictType: dictTypeList.value.find((m) => m.dictId == e)?.dictType,
  }).then((response) => {
    dictDataList.value = response.data.rows;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    iconUrl: null,
    title: null,
    websiteUrl: null,
    abstractContent: null,
    dictDataId: null,
    dictTypeId: null,
    readingCount: null,
    tags: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
  };
  proxy.resetForm("websiteRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加网址收录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getWebsite(_id).then((response) => {
    response.data.data.dictDataId = response.data.data.dictDataId + "";
    listDictData({
      pageNum: 1,
      pageSize: 1000,
      dictType: dictTypeList.value.find(
        (m) => m.dictId == response.data.data.dictTypeId
      )?.dictType,
    }).then((res) => {
      dictDataList.value = res.data.rows;
      form.value = response.data.data;
      open.value = true;
      title.value = "修改网址收录";
    });
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["websiteRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateWebsite(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWebsite(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除网址收录编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delWebsite(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "bus/website/export",
    {
      ...queryParams.value,
    },
    `website_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
